package org.bridgen.runtime.model;

import org.bridgen.runtime.util.Interval;

/**
 * 
 * @author Emma Söderberg <emma add cs.lth.se>
 *
 */

public abstract class StartIsland extends Island {

	public StartIsland(LexicalNode previous, Interval interval, String value) {
		super(previous, interval, value);
	}

	/*
	 * (non-Javadoc)
	 * @see org.briplib.recovery.Island#startOfBridge()
	 */
	@Override
	public boolean startOfBridge() {
		return true;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.briplib.recovery.Island#buildBridge(org.briplib.recovery.Island, int)
	 */
	@Override
	public Bridge buildBridge(Island target, int tol) {
		if (!hasBridge()) {
			bridge = new Bridge(this, (EndIsland)target, tol);
			target.setBridge(bridge);
		}
		return bridge;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.briplib.recovery.Island#constructFakeIsland(org.briplib.recovery.LexicalNode, boolean)
	 */
	@Override
	public Island constructFakeIsland(LexicalNode node, boolean intervalEnd) {
		if (!intervalEnd && isEndIsland(node)) {
			return (LayoutEndIsland)node;
		} else {
			Interval nodeInterval = node.getInterval();
			Interval interval = new Interval(nodeInterval.getStart(), nodeInterval.getEnd());
			EndIsland island = createEndIsland(interval);
			island.setFake(true, !intervalEnd);
			return island;
		}
	}
	
	protected abstract EndIsland createEndIsland(Interval interval);
	protected abstract boolean isEndIsland(LexicalNode node);
}
